class SingleObject:
    instance = None

    # 对象初始化时，先执行new方法，在执行init方法，new方法时分配内存空间等动作
    def __new__(cls, *args, **kwargs):
        print("new")
        if cls.instance is None:
            cls.instance = super().__new__(cls)  # 实现单例，另外这里为什么要传cls
        return cls.instance

    def __init__(self):
        print("init")


singleObject1 = SingleObject()
singleObject2 = SingleObject()
print(singleObject1)
print(singleObject2)

print("-" * 200)

# 这种方式实现的单例，会破坏继承
class SingleObject:
    instance = None

    # 对象初始化时，先执行new方法，在执行init方法，new方法时分配内存空间等动作
    def __new__(cls, *args, **kwargs):
        print("new")
        if SingleObject.instance is None:
            SingleObject.instance = super().__new__(cls)  # 实现单例，另外这里为什么要传cls
        return SingleObject.instance

    def __init__(self):
        print("init")


singleObject1 = SingleObject()
singleObject2 = SingleObject()
print(singleObject1)
print(singleObject2)